package com.example.SpringBoot_demo.controller;

import com.example.SpringBoot_demo.bean.Book;
import com.example.SpringBoot_demo.bean.BorrowHistory;
import com.example.SpringBoot_demo.bean.User;
import com.example.SpringBoot_demo.service.impl.BookServiceImpl;
import com.example.SpringBoot_demo.service.impl.BorrowServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Lombok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class BorrowController {

    @Autowired
    private BookServiceImpl bookService;
    @Autowired
    private BorrowServiceImpl borrowService;

    @GetMapping("borrow")
    public String borrow(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, HttpSession session){
        PageHelper.startPage(pageNum,10);
        List<Book> list = bookService.viewBook();
        PageInfo<Book> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        String userid=session.getAttribute("userid").toString();
        System.out.println(userid);
        return "borrow";
    }
    @GetMapping("borrowBook")
    public String borrowBook(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,HttpSession session,String bookid){
        System.out.println(bookid);
        String userid=session.getAttribute("userid").toString();
        String username=session.getAttribute("userName").toString();
        borrowService.insertBorrowHistory(userid,username,bookid);
        PageHelper.startPage(pageNum,10);
        List<Book> list = bookService.viewBook();
        PageInfo<Book> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        //String userid=session.getAttribute("userId").toString();
        //System.out.println(userid);
        return "borrow";
    }
    @GetMapping("giveBack")
    public String giveBack(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, HttpSession session){

        PageHelper.startPage(pageNum,10);
        String userid=session.getAttribute("userid").toString();
        List<BorrowHistory> list = borrowService.myBorrowHistory(userid,"pserson");
        PageInfo<BorrowHistory> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(userid);
        return "giveBack";
    }
    @GetMapping("giveBackBook")
    public String giveBackBook(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, HttpSession session,String bookid){
        String userid=session.getAttribute("userid").toString();
        borrowService.returnBook(bookid,userid);
        PageHelper.startPage(pageNum,10);
        List<BorrowHistory> list = borrowService.myBorrowHistory(userid,"person");
        PageInfo<BorrowHistory> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(userid);
        return "giveBack";
    }
    @GetMapping("personBorrowHistory")
    public String personBorrowHistory(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, HttpSession session){
        System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkk");
        PageHelper.startPage(pageNum,10);
        String userid=session.getAttribute("userid").toString();
        List<BorrowHistory> list = borrowService.myBorrowHistory(userid,"all");
        PageInfo<BorrowHistory> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(userid);
        return "personBorrowHistory";
    }
    @GetMapping("allBorrowHistory")
    public String allBorrowHistory(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, HttpSession session){
        System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkk");
        PageHelper.startPage(pageNum,10);
        String userid=null;
        List<BorrowHistory> list = borrowService.myBorrowHistory(userid,"all");
        PageInfo<BorrowHistory> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "allBorrowHistory";
    }
    @GetMapping("getBookBorrow")
    public String getUser(Model model, HttpServletRequest request, String bookId, String bookName,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<Book> book = bookService.getBook(bookId,bookName);
        PageInfo<Book> pageInfo = new PageInfo<>(book);
        model.addAttribute("pageInfo",pageInfo);
        //request.setAttribute("pageInfo.list",book);
        return "borrow";
    }
}
